package com.xintonghua.bussiness.ui.fragment.client;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.widget.ConfirmDialog;
import com.xintonghua.base.widget.MyItemTextView;
import com.xintonghua.base.widget.ShowAllListView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.AddLogAdapter;
import com.xintonghua.bussiness.adapter.XiaoFeiAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.AddLogItemBean;
import com.xintonghua.bussiness.bean.AddProductListBean;
import com.xintonghua.bussiness.bean.AddProjectBean;
import com.xintonghua.bussiness.bean.AddProjectBeans;
import com.xintonghua.bussiness.bean.AddServicePersonBean;
import com.xintonghua.bussiness.bean.Auth;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.OrderCommit;
import com.xintonghua.bussiness.bean.Persion;
import com.xintonghua.bussiness.bean.ProductBean;
import com.xintonghua.bussiness.bean.ProjectBean;
import com.xintonghua.bussiness.bean.ServerBean;
import com.xintonghua.bussiness.bean.ShopOrderListBean;
import com.xintonghua.bussiness.bean.ShopOrderMsgBean;
import com.xintonghua.bussiness.bean.ShopOrderMsgItemBean;
import com.xintonghua.bussiness.bean.SubmitLogBean;
import com.xintonghua.bussiness.ui.home.AddLogActivity;
import com.xintonghua.bussiness.ui.home.SettlementActivity;
import com.xintonghua.bussiness.util.DateUtils;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MathUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import org.feezu.liuli.timeselector.TimeSelector;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 创建订单
 */
public class XiaoFeiActivity extends BaseActivity implements DataChangeUpData, CreateOrder {

    public static final String ORDER_ID = "order_id";
    public static final String USE_TYPE = "use_type";
    public static final String PROJRCT_KEY = "project_key";
    public static final String SERVICE_KEY = "service_key";
    public static final String PRODUCT_KEY = "product_key";

    public static final String USER_ITEM_DATA = "USER_ITEM_DATA";

    @BindView(R.id.utils_time)
    MyItemTextView utilsTime;
    @BindView(R.id.lv_add_list)
    ShowAllListView lvAddList;
    @BindView(R.id.edt_nursing_sit)
    EditText edtNursingSit;
    @BindView(R.id.edt_suggest)
    EditText edtSuggest;
    @BindView(R.id.edt_told)
    EditText edtTold;
    @BindView(R.id.utils_next_time)
    MyItemTextView utilsNextTime;
    @BindView(R.id.lin_add_lay)
    LinearLayout linAddLay;
    @BindView(R.id.edt_log1)
    EditText edtLog1;
    @BindView(R.id.edt_log2)
    EditText edtLog2;
    @BindView(R.id.edt_log3)
    EditText edtLog3;
    @BindView(R.id.edt_log4)
    EditText edtLog4;
    @BindView(R.id.edt_log5)
    EditText edtLog5;
    @BindView(R.id.edt_log6)
    EditText edtLog6;
    @BindView(R.id.edt_log7)
    EditText edtLog7;
    @BindView(R.id.edt_log8)
    EditText edtLog8;
    @BindView(R.id.edt_log9)
    EditText edtLog9;
    @BindView(R.id.edt_log10)
    EditText edtLog10;
    @BindView(R.id.edt_log11)
    EditText edtLog11;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.lin_sure_lay)
    LinearLayout linSureLay;


    //    private int mOrderId;
    private int mCurUseType = 2;  // 1查看日志，2添加日志（默认添加日志布局）
    private List<EditText> mEdtList;

    private XiaoFeiAdapter mAddItemAdapter;
    //首页传过来的序列化对象
    Contant contantbean;
    List<ProjectBean> projectBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiao_fei);
        ButterKnife.bind(this);
        contantbean = (Contant) JumpUtils.getSerializable(this);
        httpCent.getCardList(contantbean.getId(), this, MyState.requestFour);
        initUI();
    }

    String tag = "AddLogActivity";

    @Override
    public void initUI() {
        super.initUI();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
//            mOrderId = bundle.getInt(ORDER_ID);
            mCurUseType = 2;//bundle.getInt(USE_TYPE);
        }
        mCurUseType = 2;
        onSimpleActionBar();
        CheckShowUI();
        mAddItemAdapter = new XiaoFeiAdapter(this, new ArrayList<AddLogItemBean>());
        lvAddList.setAdapter(mAddItemAdapter);
        mAddItemAdapter.getList().add(new AddLogItemBean());
        mAddItemAdapter.notifyDataSetChanged();

    }

    private void CheckShowUI() {
        switch (mCurUseType) {
            case 1:   //查看日志
                mEdtList = new ArrayList<>();
                mEdtList.add(edtLog1);
                mEdtList.add(edtLog2);
                mEdtList.add(edtLog3);
                mEdtList.add(edtLog4);
                mEdtList.add(edtLog5);
                mEdtList.add(edtLog6);
                mEdtList.add(edtLog7);
                mEdtList.add(edtLog8);
                mEdtList.add(edtLog9);
                mEdtList.add(edtLog10);
                mEdtList.add(edtLog11);
                linAddLay.setVisibility(View.GONE);
                linSureLay.setVisibility(View.GONE);
                for (EditText edt : mEdtList) {
                    edt.setFocusable(false);
                    edt.setClickable(false);
                }
                setSimpleActionBar("查看日志");
                requestLookData();
                break;
            case 2:
                requestLookData2();
                setSimpleActionBar("创建订单", "添加", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAddItemAdapter.getList().add(new AddLogItemBean());
                        mAddItemAdapter.notifyDataSetChanged();
                    }
                });
                break;
        }
    }

    @Override
    public void finishBack() {
        super.finishBack();
        Log.i("aaa", "finishBack");
    }

    @Override
    public void errorBack(String error, int order) {
        super.errorBack(error, order);
        Log.i("aaa", "失败" + error + "///order===>" + error);
    }

    private String dateStr;
    private String timeStr;

    @OnClick(R.id.utils_time)
    public void onClickYuYueTime() {
        TimeSelector timeSelector = new TimeSelector(this, new TimeSelector.ResultHandler() {
            @Override
            public void handle(String time) {
                dateStr = time.substring(0, time.indexOf(" "));
                timeStr = time.substring(time.indexOf(" ") + 1);
                utilsTime.setMsg(time);
            }
        }, DateUtils.getDateBeforeYear(), DateUtils.getDateAfterYear());
        timeSelector.show();
    }

    @OnClick(R.id.utils_next_time)
    public void onClickNextTime() {
        TimeSelector timeSelector = new TimeSelector(this, new TimeSelector.ResultHandler() {
            @Override
            public void handle(String time) {
                utilsNextTime.setMsg(time);
            }
        }, DateUtils.getDateBeforeYear(), DateUtils.getDateAfterYear());
        timeSelector.show();
    }

    private void requestLookData() {
//        HttpCent.getInstance(this).shopOrderMsg(AuthManager.getId(), mOrderId, this, MyState.requestOne);
    }

    //商家端查看店铺订单
    private void requestLookData2() {
//        HttpCent.getInstance(this).shopOrderMsg(AuthManager.getId(), mOrderId, this, MyState.requestL3001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            switch (requestCode) {
                case MyState.requestOne:  //添加项目
                    AddProjectBean bean = (AddProjectBean) data.getExtras().getSerializable(PROJRCT_KEY);
                    if (bean != null) {
                        mAddItemAdapter.getList().get(mAddItemAdapter.getCurPosition()).setProject(bean.getName());
                        mAddItemAdapter.getList().get(mAddItemAdapter.getCurPosition()).setPerject(bean);
                        mAddItemAdapter.getList().get(mAddItemAdapter.getCurPosition()).setFuwuxiangmuid("" + bean.getId());

                        mAddItemAdapter.notifyDataSetChanged();
                        notifySumOfMoney();
                    }
                    break;
                case MyState.requestTwo:  //添加人员
                    AddServicePersonBean bean2 = (AddServicePersonBean) data.getExtras().getSerializable(SERVICE_KEY);
                    if (bean2 != null) {
                        mAddItemAdapter.getList().get(mAddItemAdapter.getCurPosition()).setPerson(bean2.getName());
                        mAddItemAdapter.getList().get(mAddItemAdapter.getCurPosition()).setPeriosn(bean2);
                        mAddItemAdapter.getList().get(mAddItemAdapter.getCurPosition()).setPersonid("" + bean2.getId());
                        mAddItemAdapter.notifyDataSetChanged();
                        notifySumOfMoney();
                    }
                    break;
                case MyState.requestThree:  //添加产品
                    AddProductListBean bean3 = (AddProductListBean) data.getExtras().getSerializable(PRODUCT_KEY);
                    if (bean3 != null) {
                        AddLogItemBean.ItemDetailsBean itemDetailsBean3 = new AddLogItemBean.ItemDetailsBean();

                        itemDetailsBean3.setProductName(bean3.getName());
                        itemDetailsBean3.setProductId("" + bean3.getId());
                        itemDetailsBean3.setAmount("1");
                        itemDetailsBean3.setPrice("" + bean3.getPrice());
                        mAddItemAdapter.getList().get(mAddItemAdapter.getCurPosition()).getmCurUseProduct().add(itemDetailsBean3);
                        mAddItemAdapter.notifyDataSetChanged();
                        notifySumOfMoney();
                    }
                    break;

            }
        }
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        Log.i("aaa", "data===>" + data + "order====>" + order);
        switch (order) {
            case MyState.requestOne:
                ShopOrderMsgBean bean = JSONObject.parseObject(data.toString(), ShopOrderMsgBean.class);
                if (bean != null) {
                    edtLog1.setText(bean.getOrig_weight());
                    edtLog2.setText(bean.getAbdomen());
                    edtLog3.setText(bean.getArm());
                    edtLog4.setText(bean.getThigh());
                    edtLog5.setText(bean.getOperation_mode());
                    edtLog6.setText(bean.getWeight_loss());
                    edtLog7.setText(bean.getWeight_loss_catty());
                    edtLog8.setText(bean.getNow_weight());
                    edtLog9.setText(bean.getNow_abdomen());
                    edtLog10.setText(bean.getNow_arm());
                    edtLog11.setText(bean.getNow_thigh());
                }


                break;
            case MyState.requestL3001://商家端查看店铺订单
                ShopOrderMsgBean bean1 = JSONObject.parseObject(data.toString(), ShopOrderMsgBean.class);
                if (bean1 != null) {
//                   List<AddLogItemBean> serviceItems= mAddItemAdapter.getList();
                    List<ShopOrderMsgItemBean> list_shopOrderMsgItemBeans = bean1.getServiceItems();

                    for (int i = 0; i < list_shopOrderMsgItemBeans.size(); i++) {
                        ShopOrderMsgItemBean beans = list_shopOrderMsgItemBeans.get(i);
                        AddLogItemBean bean2 = new AddLogItemBean();
                        bean2.setPersonid("" + beans.getPersonalId());
                        bean2.setPerson("" + beans.getPersonalName());
                        bean2.setProject("" + beans.getUserName());
                        bean2.setFuwuxiangmuid("" + beans.getProject_id());
                        bean2.setFuwuxiangid("" + beans.getId());
                        if (!MyUtils.isNullOrEmpty(list_shopOrderMsgItemBeans.get(i).getStockProducts())) {
                            for (int j = 0; j < list_shopOrderMsgItemBeans.get(i).getStockProducts().size(); j++) {
                                ShopOrderMsgItemBean.StockProductsBean bean3 = list_shopOrderMsgItemBeans.get(i).getStockProducts().get(j);
                                AddLogItemBean.ItemDetailsBean itemDetailsBean = new AddLogItemBean.ItemDetailsBean();
//                               bean2.getmCurUseProduct();

                                itemDetailsBean.setId(bean3.getId());
                                itemDetailsBean.setProductName(bean3.getName());
                                itemDetailsBean.setAmount(bean3.getAmount());
                                itemDetailsBean.setProductId(bean3.getProduct_id());
                                itemDetailsBean.setPrice(bean3.getPrice());
//                               bean2.setmCurUseProduct(itemDetailsBean);
                                bean2.getmCurUseProduct().add(itemDetailsBean);
                            }
                        }
                        mAddItemAdapter.getList().add(bean2);
                    }
                    List<AddLogItemBean> list = chaifen(mAddItemAdapter.getList());
                    mAddItemAdapter.setList(list);
                    mAddItemAdapter.notifyDataSetChanged();
                    notifySumOfMoney();
                    if (!TextUtils.isEmpty(bean1.getSubscribe_time()))//预约时间
                        utilsTime.setMsg("" + bean1.getSubscribe_time());
                    if (!TextUtils.isEmpty(bean1.getNursing_situation()))//护理情况
                        edtNursingSit.setText("" + bean1.getNursing_situation());
                    if (!TextUtils.isEmpty(bean1.getProposal()))//建议方案
                        edtSuggest.setText("" + bean1.getProposal());
                    if (!TextUtils.isEmpty(bean1.getTold()))//叮嘱
                        edtTold.setText("" + bean1.getTold());

                    if (!TextUtils.isEmpty(bean1.getNext_nursing_time()))//下次护理时间
                        utilsNextTime.setMsg("" + bean1.getNext_nursing_time());

                    edtLog1.setText(bean1.getOrig_weight());
                    edtLog2.setText(bean1.getAbdomen());
                    edtLog3.setText(bean1.getArm());
                    edtLog4.setText(bean1.getThigh());
                    edtLog5.setText(bean1.getOperation_mode());
                    edtLog6.setText(bean1.getWeight_loss());
                    edtLog7.setText(bean1.getWeight_loss_catty());
                    edtLog8.setText(bean1.getNow_weight());
                    edtLog9.setText(bean1.getNow_abdomen());
                    edtLog10.setText(bean1.getNow_arm());
                    edtLog11.setText(bean1.getNow_thigh());

                }
                break;
            case MyState.requestL3000://仅仅保存
                Log.i("aaa", "保存成功");
                try {

                    MyUtils.mToast(XiaoFeiActivity.this, "保存成功");
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("aaa", "json解析错误1");
                }

                break;
            case MyState.requestL3002://保存后进入结算
                try {
                    MyUtils.mToast(XiaoFeiActivity.this, "保存成功");

                    List<AddLogItemBean> list = mAddItemAdapter.getList();
                    List<SubmitLogBean> sublist = AddLogItemBeanToSubmitLogBean(list);
                    List<SubmitLogBean> subli = hebing(sublist);
                    Bundle bun = new Bundle();
                    bun.putSerializable("data", (Serializable) subli);
                    bun.putString("money", tvAllMoney.getText().toString().trim() + "");
                    bun.putSerializable("homeitem", contantbean);
                    openActivity(SettlementActivity.class, bun);
//                    homeOrderInfoBean
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("aaa", "json解析错误1");
                }
                break;
            case MyState.requestFour:
                projectBean = JSONObject.parseArray((String) data, ProjectBean.class);
                Log.e("data", String.valueOf(data));
                break;
        }
    }


    @OnClick(R.id.tv_sure)
    public void sendOrder() {

        ConfirmDialog.showDialog(this, "提示", "是否创建订单？", "取消", "保存", null, new ConfirmDialog.OnRightListener() {
            @Override
            public void onClick(ConfirmDialog dialog) {
                createOrder();

            }
        });


    }

    @Override
    public String listTojson(List<SubmitLogBean> sublist) {
        org.json.JSONArray jsonArray = new org.json.JSONArray();
        for (int i = 0; i < sublist.size(); i++) {
            try {
                org.json.JSONObject json1 = new org.json.JSONObject();
                json1.put("serviceItemId", "" + sublist.get(i).getServiceItemId());
                json1.put("projectId", "" + sublist.get(i).getProjectId());
                json1.put("servicePersonalId", "" + sublist.get(i).getServicePersonalId());
                org.json.JSONArray array = new org.json.JSONArray();
                for (int j = 0; j < sublist.get(i).getItemDetails().size(); j++) {
                    org.json.JSONObject obj3 = new org.json.JSONObject();
                    obj3.put("id", "" + sublist.get(i).getItemDetails().get(j).getId());
                    obj3.put("productId", "" + sublist.get(i).getItemDetails().get(j).getProductId());
                    obj3.put("amount", "" + sublist.get(i).getItemDetails().get(j).getAmount());
                    Log.i("aaa", "ssss" + sublist.get(i).getItemDetails().get(j).getId());
                    Log.i("aaa", "ssss2" + sublist.get(i).getItemDetails().get(j).getProductId());
                    Log.i("aaa", "ssss3" + sublist.get(i).getItemDetails().get(j).getAmount());
                    array.put(obj3);
                }
                json1.put("itemDetails", array.toString());
                jsonArray.put(json1.toString());
                return jsonArray.toString();
            } catch (Exception e) {
                e.printStackTrace();
                Log.i(tag, "json解析出错");
            }
        }

        return "";


    }

    @Override
    public List<SubmitLogBean> AddLogItemBeanToSubmitLogBean(List<AddLogItemBean> list) {
        List<SubmitLogBean> sublist = new ArrayList<>();//吧list中的数据全部放进来
        for (int i = 0; i < list.size(); i++) {
            SubmitLogBean bean = new SubmitLogBean();
            AddLogItemBean add = list.get(i);
            bean.setServicePersonalId(add.getPersonid());//服务人员编号
            bean.setServiceItemId(add.getFuwuxiangid());//服务项id
            bean.setProjectId(add.getFuwuxiangmuid());//服务项目id
            bean.setPerson(add.getPerson());

            if (!MyUtils.isNullOrEmpty(add.getmCurUseProduct())) {
                for (int j = 0; j < add.getmCurUseProduct().size(); j++) {
                    SubmitLogBean.ItemDetailsBean beansss = new SubmitLogBean.ItemDetailsBean();

                    beansss.setId(add.getmCurUseProduct().get(j).getId());
                    beansss.setProductId(add.getmCurUseProduct().get(j).getProductId());
                    beansss.setProductName(add.getmCurUseProduct().get(j).getProductName());
                    beansss.setAmount(add.getmCurUseProduct().get(j).getAmount());
                    beansss.setPrice(add.getmCurUseProduct().get(j).getPrice());
                    bean.getItemDetails().add(beansss);
                }

            }
            sublist.add(bean);
        }
        return sublist;
    }

    @Override
    public List<SubmitLogBean> hebing(List<SubmitLogBean> beans) {
        List<SubmitLogBean> list = beans;

        for (int i = 0; i < list.size(); i++) {

            if (!MyUtils.isNullOrEmpty(list.get(i).getItemDetails())) {
                HashMap<String, SubmitLogBean.ItemDetailsBean> tabMap = new HashMap<>();
                //把相同的商品提取出来，然后相加
                for (int j = 0; j < list.get(i).getItemDetails().size(); j++) {

                    SubmitLogBean.ItemDetailsBean itemDetailsBean = list.get(i).getItemDetails().get(j);
                    //如果有相同的产品
                    if (tabMap.containsKey(itemDetailsBean.getProductId())) {
                        SubmitLogBean.ItemDetailsBean itemDetailsBean2 = tabMap.get(itemDetailsBean.getProductId());
                        BigDecimal a = MathUtils.add(itemDetailsBean.getAmount(), itemDetailsBean2.getAmount());
                        itemDetailsBean2.setAmount(a.toString());
                        tabMap.put(itemDetailsBean2.getProductId(), itemDetailsBean2);
                    } else {
                        tabMap.put(itemDetailsBean.getProductId(), itemDetailsBean);
                    }
                }
                if (tabMap != null) {
                    List<SubmitLogBean.ItemDetailsBean> item = list.get(i).getItemDetails();
                    List<SubmitLogBean.ItemDetailsBean> items = new ArrayList<>();
                    for (String key : tabMap.keySet()) {
                        items.add(tabMap.get(key));//  map.get(key);
                        Log.i(tag, "map中的key  tabMap.get(key)ProductId=" + tabMap.get(key).getProductId());
                        Log.i(tag, "map中的key  tabMap.get(key)Amount=" + tabMap.get(key).getAmount());
                        Log.i(tag, "map中的key  tabMap.get(key)Id=" + tabMap.get(key).getId());
                        Log.i(tag, "map中的key  tabMap.get(key)getProductName=" + tabMap.get(key).getProductName());
                    }
                    list.get(i).setItemDetails(items);
                }
            }
        }
        return list;
    }

    @Override
    public List<AddLogItemBean> chaifen(List<AddLogItemBean> bean) {

        List<AddLogItemBean> bbbean = bean;
        try {
            for (int i = 0; i < bean.size(); i++) {
                for (int j = 0; j < bean.get(i).getmCurUseProduct().size(); j++) {
                    int mm = Integer.parseInt(bean.get(i).getmCurUseProduct().get(j).getAmount());
                    while (mm > 1) {
                        AddLogItemBean.ItemDetailsBean beanss = new AddLogItemBean.ItemDetailsBean();
                        beanss.setAmount("1");
                        beanss.setProductId(bean.get(i).getmCurUseProduct().get(j).getProductId());
                        beanss.setProductName(bean.get(i).getmCurUseProduct().get(j).getProductName());
                        beanss.setId(bean.get(i).getmCurUseProduct().get(j).getId());
                        beanss.setPrice(bean.get(i).getmCurUseProduct().get(j).getPrice());
                        bbbean.get(i).getmCurUseProduct().add(beanss);
                        mm--;
                    }
                    bbbean.get(i).getmCurUseProduct().get(j).setAmount("1");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bbbean;
    }

    @Override
    public void Submit(int type) {
        if (mAddItemAdapter.getList().size() == 0) {
            mToast("请添加服务");
            return;
        }
        OrderCommit commit = new OrderCommit();
        List<ProjectBean> cardList = new ArrayList<>();
        List<AddLogItemBean> list = mAddItemAdapter.getList();
        for (AddLogItemBean itemBean : list) {
            for (ProjectBean bean : projectBean) {
                if (Integer.valueOf(itemBean.getFuwuxiangmuid()) == bean.getId()) {
                    cardList.add(bean);
                }
            }
        }
        commit.setCardList(cardList);
        commit.setDate(dateStr);
        commit.setTime(timeStr);
        commit.setExplain("");
        commit.setShop_id(AuthManager.geteAuth().getShop_id());
        commit.setTotalPrice(Double.valueOf(money));
        commit.setUser_id(AuthManager.getId());
        List<ServerBean> serverBeanList = new ArrayList<>();
        for (AddLogItemBean itemBean : list) {
            ServerBean serverBean = new ServerBean();
            Persion persion = new Persion(itemBean.getPeriosn().getId(), 0, itemBean.getPeriosn().getName(), "0");
            serverBean.setPeople(persion);
            serverBean.setAdd(true);
            // serverBean.setProjectBean(itemBean.getPerject());
            serverBean.setProducts(itemBean.getmCurUseProduct());
            serverBeanList.add(serverBean);
        }
        commit.setServerList(serverBeanList);
        httpCent.orderCommit(JSONObject.toJSONString(commit), this, MyState.requestL3000);


//        Auth auth = AuthManager.geteAuth(this);
//        int userid = auth.getId();
//        HashMap<String, String> map = new HashMap<>();
//        map.put("userid", "" + auth.getId());
////        map.put("shopServiceOrderId", "" + mOrderId);
////        Auth auth = JSONObject.parseObject((String) data, Auth.class);
//        List<AddLogItemBean> list = mAddItemAdapter.getList();
//        List<SubmitLogBean> sublist = AddLogItemBeanToSubmitLogBean(list);
//
//        List<SubmitLogBean> subdata = hebing(sublist);
//
////        Log.i("aaa","list中的数据===>"+sublist.toString()+"list大小==》"+sublist.size());
//        String s = JsonUtil.getJsonStringByList(subdata);//listTojson(subdata);//.toJSONString(sublist,SerializerFeature.DisableCircularReferenceDetect);
//
//        map.put("shopServiceItems", "" + s);
//        map.put("nursingSituation", "" + edtNursingSit.getText().toString().trim());//护理情况
//        map.put("proposal", "" + edtSuggest.getText().toString().trim());//建议方案
//        map.put("told", "" + edtTold.getText().toString().trim());//特别叮嘱
//        map.put("nextNursingTime", "" + utilsNextTime.getMsg());//下次护理时间
//        map.put("origWeight", "" + edtLog1.getText().toString().trim());//原始体重
//        map.put("abdomen", "" + edtLog2.getText().toString().trim());
//        map.put("arm", "" + edtLog3.getText().toString().trim());
//        map.put("thigh", "" + edtLog4.getText().toString().trim());
//        map.put("operationMode", "" + edtLog5.getText().toString().trim());
//        map.put("weightLoss", "" + edtLog6.getText().toString().trim());
//        map.put("weightLossCatty", "" + edtLog7.getText().toString().trim());
//        map.put("nowWeight", "" + edtLog8.getText().toString().trim());
//        map.put("nowAbdomen", "" + edtLog9.getText().toString().trim());
//        map.put("nowArm", "" + edtLog10.getText().toString().trim());
//        map.put("nowThigh", "" + edtLog11.getText().toString().trim());
//
//        Log.i("aaa", "shopServiceItems内容" + s);
//        Log.i("aaa", "map内容" + JsonUtil.getJsonStringByEntity(map));//.toJSONString(map));
//        if (type == 0)
//            HttpCent.getInstance(this).shopAddLog(map, XiaoFeiActivity.this, MyState.requestL3000);
//        if (type == 1)
//            HttpCent.getInstance(this).shopAddLog(map, XiaoFeiActivity.this, MyState.requestL3002);


    }

    String money;

    @Override
    public void notifySumOfMoney() {

        money = getIntent().getStringExtra("fuwufei");//服務費先固定100，後面要改再改這裡

        money = "0";
        Log.i("aaa", "583money===>" + money);
        if (!MyUtils.isNullOrEmpty(mAddItemAdapter.getList()))
            for (int i = 0; i < mAddItemAdapter.getList().size(); i++) {
                if (!MyUtils.isNullOrEmpty(mAddItemAdapter.getList().get(i).getmCurUseProduct()))
                    for (int j = 0; j < mAddItemAdapter.getList().get(i).getmCurUseProduct().size(); j++) {

                        String m1 = mAddItemAdapter.getList().get(i).getmCurUseProduct().get(j).getPrice();
                        Log.i("aaa", "592===>" + m1);
                        String m2 = mAddItemAdapter.getList().get(i).getmCurUseProduct().get(j).getAmount();
                        if (TextUtils.isEmpty(m2)) {
                            m2 = "1";
                        }
                        Log.i("aaa", "594amount===>" + m2);
                        String m3 = MathUtils.strMul2(m1, m2, 2);
                        Log.i("aaa", "m3===>" + m3);
                        money = MathUtils.strAdd(money, m3, 2);
                    }
            }
        List<AddLogItemBean> list = mAddItemAdapter.getList();
        for (AddLogItemBean bean : list) {
            money = MathUtils.strAdd(money, bean.getPerject().getMoney() + "", 2);
        }
        Log.i("aaa", "596money===>" + money);
        tvAllMoney.setText("共计消费：" + money);
    }

    @Override
    public void createOrder() {
        if (mAddItemAdapter.getList().size() == 0) {
            mToast("请添加服务");
            return;
        }
        if (TextUtils.isEmpty(money)) {
            mToast("请添加服务");
            return;
        }
        OrderCommit commit = new OrderCommit();
        List<ProjectBean> cardList = new ArrayList<>();
        List<AddLogItemBean> list = mAddItemAdapter.getList();
        for (AddLogItemBean itemBean : list) {
            for (ProjectBean bean : projectBean) {
                if (TextUtils.isEmpty(itemBean.getFuwuxiangmuid())) {
                    break;
                }
                if (Integer.valueOf(itemBean.getFuwuxiangmuid()) == bean.getProject_id()) {
                    cardList.add(bean);
                }
            }
        }
        commit.setCardList(cardList);
        commit.setDate(dateStr);
        commit.setTime(timeStr);
        commit.setExplain("");
        commit.setShop_id(AuthManager.geteAuth().getShop_id());
        commit.setTotalPrice(Double.valueOf(money));
        commit.setUser_id(contantbean.getId());
        List<ServerBean> serverBeanList = new ArrayList<>();
        for (AddLogItemBean itemBean : list) {
            ServerBean serverBean = new ServerBean();
            Persion persion = new Persion(itemBean.getPeriosn().getId(), 0, itemBean.getPeriosn().getName(), "0");
            serverBean.setPeople(persion);
            serverBean.setAdd(true);
            AddProjectBeans addProjectBean = new AddProjectBeans();
            addProjectBean.setAsideIndex(0);
            addProjectBean.setId(itemBean.getPerject().getId());
            addProjectBean.setIndex(0);
            addProjectBean.setName(itemBean.getPerject().getName());
            addProjectBean.setPrice(itemBean.getPerject().getMoney());
            serverBean.setItems(addProjectBean);
            List<AddLogItemBean.ItemDetailsBean> itemDetailsBeans = new ArrayList<>();
            for (AddLogItemBean.ItemDetailsBean bean : itemBean.getmCurUseProduct()) {
                AddLogItemBean.ItemDetailsBean bean1 = new AddLogItemBean.ItemDetailsBean();
                bean1.setAmount(bean.getAmount());
                bean1.setAsideIndex(bean.getAsideIndex());
                bean1.setId(bean.getProductId());
                bean1.setNum(bean.getNum());
                bean1.setIndex(0);
                bean1.setPrice(bean.getPrice());
                bean1.setProductId(bean.getProductId());
                bean1.setProductName(bean.getProductName());
                itemDetailsBeans.add(bean1);
            }
            serverBean.setProducts(itemDetailsBeans);
            serverBeanList.add(serverBean);
        }
        commit.setServerList(serverBeanList);
        httpCent.orderCommit(JSONObject.toJSONString(commit), this, MyState.requestL3000);

    }
}

interface DataChangeUpData {
    //list转json
    String listTojson(List<SubmitLogBean> sublist);

    //将AddLogItemBean转成SubmitLogBean
    List<SubmitLogBean> AddLogItemBeanToSubmitLogBean(List<AddLogItemBean> beans);

    //将所有商品重复的部分提取出来 然后让他们的产品数量增加一个
    List<SubmitLogBean> hebing(List<SubmitLogBean> bean);

    //将所有商品数量，如2个拆成1个一个的
    List<AddLogItemBean> chaifen(List<AddLogItemBean> bean);

    void Submit(int type);

    //更新价格
    void notifySumOfMoney();
}

interface CreateOrder {

    void createOrder();
}